from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from utils.config_loader import load_settings

settings = load_settings()
DATABASE_URI = settings['database']['uri']

_engine = create_engine(
    DATABASE_URI,
    pool_pre_ping=True,
    pool_size=10,
    max_overflow=20,
    echo=False
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=_engine)

def get_session():
    """
    获取一个 SQLAlchemy Session，使用完后请调用 .close()
    """
    return SessionLocal()

def get_connection():
    """
    返回一个原生 Connection，可用于 pandas.read_sql 或执行原生 SQL。
    """
    return _engine.connect()
